package com.qsmx.store.app.main.mem;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;

import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.util.StringUtil;
import com.qsmx.store.core.delegates.AtmosDelegate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MemSaleCardChildDelegate extends AtmosDelegate {

    private ArrayAdapter arrayAdapter;
    private List<String> mList = new ArrayList<>();

    @BindView(R2.id.et_ic_num)
    AppCompatEditText etICNum = null;
    @BindView(R2.id.tv_read_card)
    AppCompatTextView tvReadCard = null;
    @BindView(R2.id.et_user_name)
    AppCompatEditText etUserName = null;
    @BindView(R2.id.sp_sex)
    AppCompatSpinner spSex = null;
    @BindView(R2.id.et_mem_card_num)
    AppCompatEditText etMemCardNum = null;
    @BindView(R2.id.et_phone)
    AppCompatEditText etPhone = null;
    @BindView(R2.id.sp_mem_type)
    AppCompatSpinner spMemType = null;
    @BindView(R2.id.et_person_num)
    AppCompatEditText etPersonNum = null;
    @BindView(R2.id.et_mail)
    AppCompatEditText etMail = null;
    @BindView(R2.id.et_out_mem_num)
    AppCompatEditText etOutMemNum = null;
    @BindView(R2.id.tv_birthday)
    AppCompatTextView tvBirthday = null;
    @BindView(R2.id.iv_date)
    AppCompatImageView ivDate = null;
    @BindView(R2.id.et_old_ticket_num)
    AppCompatEditText etOldTicketNum = null;
    @BindView(R2.id.et_old_coin_num)
    AppCompatEditText etOldCoinNum = null;
    @BindView(R2.id.et_area)
    AppCompatEditText etArea = null;
    @BindView(R2.id.et_address_1)
    AppCompatEditText etAddress1 = null;
    @BindView(R2.id.et_address_2)
    AppCompatEditText etAddress2 = null;
    @BindView(R2.id.et_community)
    AppCompatEditText etCommuntiy = null;
    @BindView(R2.id.et_customer)
    AppCompatEditText etCustomer = null;
    @BindView(R2.id.et_remarks)
    AppCompatEditText etRemarks = null;

    @OnClick(R2.id.tv_cancel)
    void onCancel() {
        getParentDelegate().getParentDelegate().pop();
    }

    @OnClick(R2.id.tv_sure_sale)
    void onSureSale() {

    }

    @OnClick(R2.id.iv_date)
    void onSetBirthday() {
        doSetBirthDay();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_mem_card_sale_child;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        initView();
    }

    private void initView() {
        mList.add("test1");
        mList.add("test2");
        mList.add("test3");
        mList.add("test4");

        arrayAdapter = new ArrayAdapter(getContext(), R.layout.item_mem_card_spinner_select, mList);
        arrayAdapter.setDropDownViewResource(R.layout.item_spinner_drop);
        spSex.setAdapter(arrayAdapter);
        spMemType.setAdapter(arrayAdapter);
    }

    private void doSetBirthDay() {

        final Calendar mBirthday = Calendar.getInstance();

        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTimeInMillis(System.currentTimeMillis());
        int nowYear = nowCalendar.get(Calendar.YEAR);
        int nowMonthOfYear = nowCalendar.get(Calendar.MONTH);
        int nowDayOfMonth = nowCalendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(getContext(), R.style.MyDatePickerDialogTheme, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                mBirthday.set(Calendar.YEAR, year);
                mBirthday.set(Calendar.MONTH, monthOfYear);
                mBirthday.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                tvBirthday.setText(StringUtil.getDateTimeToString(mBirthday.getTime()));
            }
        }, nowYear, nowMonthOfYear, nowDayOfMonth);
        dpd.getDatePicker().setSpinnersShown(false);
        dpd.show();


    }
}
